package com.qys.livesMall.shop.event.handle.service;

import com.alibaba.fastjson2.JSONObject;
import com.qys.livesMall.common.exception.QysException;
import com.qys.livesMall.shop.event.handle.BaseHandler;
import com.qys.livesMall.shop.event.shop.pojo.ShopWaitWlPojo;
import com.qys.livesMall.shop.modal.entity.H2ShopStatisticsInfo;
import com.qys.livesMall.shop.modal.entity.ShopEventInfo;
import com.qys.livesMall.shop.service.H2ShopRechargeWithdrawalService;
import com.qys.livesMall.shop.service.H2ShopStatisticsInfoService;
import com.qys.livesMall.shop.service.ShopStatisticsEverydayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;

/**
 * @menu ShopReWlServer
 * @date 2023/11/03 15:23
 */
@Service
public class ShopWaitWlServer implements BaseHandler<ShopWaitWlPojo> {

    @Autowired
    private H2ShopStatisticsInfoService shopStatisticsInfoService;

    @Autowired
    private H2ShopRechargeWithdrawalService rechargeWithdrawalService;

    @Autowired
    private ShopStatisticsEverydayService shopStatisticsEverydayService;
    @Override
    public void correct(Long shopId) {

    }

    @Override
    public void handle(ShopEventInfo<ShopWaitWlPojo> info) {
        H2ShopStatisticsInfo shopStatisticsInfo=shopStatisticsInfoService.getField(info.getShopId(),H2ShopStatisticsInfo::getId, H2ShopStatisticsInfo::getWaitWithdrawal);
        if (shopStatisticsInfo==null){
            throw new QysException("未发现店铺："+info.getShopId()+"的统计数据");
        }
        if (info.getParamObj().getAmount()==null){
            throw new QysException("统计店铺待提现错误："+ JSONObject.toJSONString(info));
        }
        if (shopStatisticsInfo.getWaitWithdrawal()==null){
            shopStatisticsInfo.setWaitWithdrawal(BigDecimal.ZERO);
        }

        if (info.getParamObj().getType()==1) {
            shopStatisticsInfo.setWaitWithdrawal(shopStatisticsInfo.getWaitWithdrawal().add(info.getParamObj().getAmount()));
        }else if (info.getParamObj().getType()==0){
            shopStatisticsInfo.setWaitWithdrawal(shopStatisticsInfo.getWaitWithdrawal().subtract(info.getParamObj().getAmount()));
            if (shopStatisticsInfo.getWaitWithdrawal().compareTo(BigDecimal.ZERO)<0){
                return;
            }
        }

        shopStatisticsInfoService.updateById(shopStatisticsInfo);
    }
}

